$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var focus = true;
    var pass = 1;

    $("div.page").on("click", "span.personal", function() {
        localStorage.removeItem("packMachine");
        focus = false;
        $("#goods_id").blur();
    });

    $(".panel-overlay").on("click", function () {
        focus = true;
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1000);
    });

    $("#goods_id").focus();

    //点击 刷新 按钮  重新加载页面
    $(".page .head").on("click", "span.icon-refresh-1-copy", function () {
        window.location.href = "acidTransfer.html";
    });

    //扫描调拨单号
    $("#goods_id").on("keyup", function (event) {
        if (event.keyCode == "13") {
            var code = $(this).val();
            if (!code) return $alert("调拨单号不可为空");
            getAcidTransferorders(code);
        }
    });

    //获取调拨单列表
    getAcidTransferorders("");

    function getAcidTransferorders(code) {
        //清空页面中的发货单
        $("#work_list").find("li").remove();
        //加载提示符 显示
        $(".content .list-block ul#work_list .infinite-scroll-preloader").show();
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            url: url + "/app/acidTransfer/getAcidTransferorders",
            type: "post",
            data: {
                code: code
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                if (res.success) {
                    var data = res.result;
                    //清空页面中的发货单
                    $("#work_list").find("li").remove();
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    //console.log(data);
                    if (data.length == 0) return $alert("暂无酸洗调拨单");
                    var data_html = '';
                    for (var i = 0; i < data.length; i++) {
                        data_html += '<li>' +
                            '<p class="li_title">调拨单号：<span class="sp work_code">' + data[i].orderNumber + '</span><span class="icon iconfont icon-right pull-right"></span></p>' +
                            '<div class="row no-gutter li_content"><div class="col-100">' +
                            '<p class="p_none"><span>id:</span><span class="sp goods_id">' + data[i]._id + '</span></p>' +
                            '<p class="p_none"><span>productCode:</span><span class="sp goods_productCode">' + data[i].inventoryCode + '</span></p>' +
                            '<div class="details">' +
                            '<p><span>调拨前车间:</span><span class="sp goods_house">' + data[i].transferShopBefore + '</span></p>' +
                            '<p><span>调拨后车间:</span><span class="sp goods_house">' + data[i].transferShopAfter + '</span></p>' +
                            '<p><span>编码:</span><span class="sp goods_productCode">' + data[i].inventoryCode + '</span></p>' +
                            '<p><span>名称:</span><span class="sp goods_productName">' + data[i].inventoryName + '</span></p>' +
                            '<p><span>规格:</span><span class="sp goods_productSpecification">' + data[i].inventorySpecification + '</span>' +
                            '<span>钢号:</span><span class="sp goods_inventorySteel">' + (data[i].inventorySteel || "") + '</span></p>' +
                            '<p><span>剩余调拨:</span><span class="sp goods_planCount">' + (accSub(data[i].transferCount, data[i].actualTransferCount || 0)) + '</span>件' +
                            '<span></span><span class="sp goods_planWeight">' + (accSub(data[i].transferWeights, data[i].actualTransferWeights || 0)) + '</span>T</p>' +
                            '<p><span>制单日期:</span><span class="sp goods_createDate">' + data[i].createTime.split(" ")[0] + '</span></p>';
                        if (data[i].result && data[i].result.bill_no) {
                            data_html += '<p class="p_none"><span class="sp goods_QR">' + data[i].result.QR + '</span></p>' +
                                '<p><span>运单号:</span><span class="sp goods_bill_no">' + data[i].result.bill_no + '</span></p>' +
                                '<p><span>车牌:</span><span class="sp goods_plate_no">' + data[i].result.plate_no + '</span>' +
                                '<span>司机:</span><span class="sp goods_driver_name">' + data[i].result.driver_name + '</span></p>';
                        }
                        if (data[i].comment) {
                            data_html += '<p><span>备注:</span><span class="sp comment">' + data[i].comment + '</span></p>';
                        }
                        data_html += '</div></div></div></li>';
                    }
                    $("#work_list").append(data_html);
                    //改变 共计 数
                    $(".content div.ul_foot span.sp_js").html($("#work_list li").length);
                    $("#goods_id").val("");
                } else {
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    $alert(res.message);
                }
            },
            error: function (error) {
                //隐藏提示符 显示
                $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                if (error.status == 0) {
                    $alert("请求超时，请再试一次！");
                } else {
                    console.log(error);
                    $alert(error.responseText);
                }
            }
        });
    }

    //点击 发货单箭头
    var work = "";
    $(".content #work_list").on("click", "li .li_title", function () {
        var id = $(this).parent().find("span.goods_id").html();
        var code = $(this).parent().find("span.work_code").html();
        var productName = $(this).parent().find("span.goods_productName").html();
        var productSpecification = $(this).parent().find("span.goods_productSpecification").html();
        var planCount = $(this).parent().find("span.goods_planCount").html();
        var planWeight = $(this).parent().find("span.goods_planWeight").html();
        var QR = $(this).parent().find("span.goods_QR").html();
        if (!QR || QR == "空") {
            $alert("请先扫描司机二维码！");
            $("#goods_id").val("").focus();
            return
        }
        loading_show("选择中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + "/app/acidTransfer/selectAcidTransfer",
            data: {
                _id: id,
                QR: QR
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                if (res.success) {
                    loading_hide();
                    var work = {
                        // locationInventorys: data,
                        id: id,
                        code: code,
                        productName: productName,
                        productSpecification: productSpecification,
                        planCount: planCount,
                        planWeight: planWeight
                    };
                    work.locationInventorys = res.result;
                    work.driver_qrcode_bill_no = res.driver_qrcode.bill_no;
                    work.driver_qrcode_count = res.driver_qrcode.count;
                    localStorage.setItem("work", JSON.stringify(work));
                    window.location.href = "acidTransfer_one.html";
                } else {
                    loading_hide();
                    $alert(res.message);
                    $("div.div_huanban input.QR").val("").focus();
                }
            },
            error: function (error) {
                loading_hide();
                if (error.status == 0) {
                    //loading_hide();
                    $alert("请求超时，请重新操作！");
                } else {
                    //loading_hide();
                    $alert(error.responseText);
                }
            }
        });
        // work = {
        //     // locationInventorys: data,
        //     id: id,
        //     code: code,
        //     productName: productName,
        //     productSpecification: productSpecification,
        //     planCount: planCount,
        //     planWeight: planWeight
        // };
        // $(".mengceng").show();
        // $("div.div_huanban").show();
        // $("div.div_huanban input.QR").val("").focus();
    });
    /*//点击 取消
    $("div.div_huanban .hb_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_huanban").hide();
    });
    //点击 确认
    $("div.div_huanban input.QR").on("keyup", function (event) {
        if (event.keyCode == "13") {
            selectAcidTransfer();
        }
    });
    $("div.div_huanban .hb_queding").click(function () {
        selectAcidTransfer();
    });
    function selectAcidTransfer() {
        if (!$("div.div_huanban input.QR").val()) {
            $alert("请扫描司机二维码！");
            $("div.div_huanban input.QR").val("").focus();
        } else {
            loading_show("选择中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + "/app/acidTransfer/selectAcidTransfer",
                data: {
                    _id: work.id,
                    QR: $("div.div_huanban input.QR").val()
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                timeout: 60000,
                success: function (res) {
                    if (res.success) {
                        loading_hide();
                        work.locationInventorys = res.result;
                        work.driver_qrcode_bill_no = res.driver_qrcode.bill_no;
                        work.driver_qrcode_count = res.driver_qrcode.count;
                        localStorage.setItem("work", JSON.stringify(work));
                        window.location.href = "acidTransfer_one.html";
                    } else {
                        loading_hide();
                        $alert(res.message);
                        $("div.div_huanban input.QR").val("").focus();
                    }
                },
                error: function (error) {
                    loading_hide();
                    if (error.status == 0) {
                        //loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        //loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        }
    }*/

});